package com.xigua.xcar.helper;

import android.location.Location;
import android.util.Log;

public class CalDistance {
	private static final String tag = "CalDistance";
	public static  double getDistance(double lat1, double lon1,  
					double lat2, double lon2) {  
	        float[] results=new float[1];  
	        Location.distanceBetween(lat1, lon1, lat2, lon2, results);  
	        return results[0];  
	}
	
	private static final double EARTH_RADIUS = 6378137.0; 
	
	public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
			lat_a = lat_a/1e6;
			lng_a = lng_a/1e6;
			lat_b = lat_b/1e6;
			lng_b = lng_b/1e6;
			Log.i(tag, "lat_a:" + lat_a + ",lng_a:" + lng_a + ",lat_b:" + lat_b + ",lng_b:" + lng_b);
	       double radLat1 = (lat_a * Math.PI / 180.0);
	       double radLat2 = (lat_b * Math.PI / 180.0);
	       double a = radLat1 - radLat2;
	       double b = (lng_a - lng_b) * Math.PI / 180.0;
	       double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
	              + Math.cos(radLat1) * Math.cos(radLat2)
	              * Math.pow(Math.sin(b / 2), 2)));
	       s = s * EARTH_RADIUS;
	       s = Math.round(s * 10000) / 10000;
	       return s;
	    }
	
	public static double  distance(double lat1, double lon1, double lat2, double lon2) 
	{
		double theta = lon1 - lon2;
		double dist = Math.sin(dex2rad(lat1)) * Math.sin(dex2rad(lat2))
						+ Math.cos(dex2rad(lat1)) * Math.cos(dex2rad(lat2))
						* Math.cos(dex2rad(theta));
				dist = Math.acos(dist);
				dist = rad2deg(dist);
				double miles = dist * 60 * 1.1515;
				return miles;
	}
	
	//将角度转换为弧度
	static double dex2rad(double degree){
		return degree / 180 * Math.PI;
	}
	
	//将弧度转换为角度
	static double rad2deg(double radian){
		return radian*180 /Math.PI;
	}
	
}
